const SqliteUtil = appRequire('modules/SqliteUtil');
const PageResultVo = appRequire('vo/PageResultVo.js');
class SysRoleSqliteDao {
  constructor() {

  }
  static async create() {
    let db = await SqliteUtil.open(appConfig.dbPath);
    await db.run(`CREATE TABLE "sys_role" (
      "id" VARCHAR(64) NOT NULL,
      "role_name" VARCHAR(64) NOT NULL UNIQUE DEFAULT '',
      "sys_role_auth_ids" VARCHAR(2048) DEFAULT '',
      "remarks" VARCHAR(255) DEFAULT '',
      "create_time" VARCHAR(255) DEFAULT '',
      "update_time" VARCHAR(255) DEFAULT '',
      "is_used" VARCHAR(10) DEFAULT '0',
      "is_hide" VARCHAR(10) DEFAULT '0',
      "sort" integer DEFAULT 0,
      PRIMARY KEY ("id")
    );`);
    await db.close();
  }
  static async saveOne(role) {
    let db = await SqliteUtil.open(appConfig.dbPath);
    try {
      await db.run(`insert into sys_role
          (id,role_name,sys_role_auth_ids,remarks,create_time,update_time,sort) 
          values(?,?,?,?,?,?)`,
        [role.id, role.roleName,role.authIds,role.remarks,role.createTime, role.updateTime,role.sort]);
    } catch (error) {
      throw new Error(error);
    } finally {
      await db.close();
    }
  }
  static async updateOne(role) {
    let db = await SqliteUtil.open(appConfig.dbPath);
    try {
      await db.run(`update sys_role 
        set role_name=?,sys_role_auth_ids=?,remarks=?,update_time=?,is_used=?,is_hide=?,sort=? where id = ?`,
      [role.roleName,role.authIds,role.remarks, role.updateTime, role.isUsed, role.isHide, role.sort,role.id]);
    } catch (error) {
      throw new Error(error);
    } finally {
      await db.close();
    }
  }
  static async deleteById(id) {
    let db = await SqliteUtil.open(appConfig.dbPath);
    try {
      await db.run(`delete from sys_role where id = ?`,
      [id]);
    } catch (error) {
      throw new Error(error);
    } finally {
      await db.close();
    }
  }
  static async deleteMany(ids) {
    let db = await SqliteUtil.open(appConfig.dbPath);
    try {
      console.log(ids);
      let sqlWhere = 'where id in (';
      for(let i = 0;i<ids.length;i++){
        if(i!=ids.length-1){
          sqlWhere+=`'${ids[i]}',`
        }else{
          sqlWhere+=`'${ids[i]}')`
        }
      }
      let sql = `delete from sys_role ${sqlWhere}`;
      console.log(sql);
      await db.run(sql);
    } catch (error) {
      throw new Error(error);
    } finally {
      await db.close();
    }
  }
  static async findOne(id) {
    let db = await SqliteUtil.open(appConfig.dbPath);
    try {
      let data = await db.get(`select id,role_name as roleName,sys_role_auth_ids as authIds,remarks,sort from sys_role where id=?`,[id]);
      return data;
    } catch (error) {
      throw new Error(error);
    } finally {
      await db.close();
    }
  }
  static async findById(id) {
    let db = await SqliteUtil.open(appConfig.dbPath);
    try {
      let data = await db.get(`select * from sys_role where id=?`,[id]);
      return data;
    } catch (error) {
      throw new Error(error);
    } finally {
      await db.close();
    }
  }
  static async findAll() {
    let db = await SqliteUtil.open(appConfig.dbPath);
    try {
      let data = await SqliteUtil.each(db,`select id,role_name as roleName from sys_role order by sort desc`);
      return data;
    } catch (error) {
      throw new Error(error);
    } finally {
      await db.close();
    }
  }
  static async findByPage(page) {
    let db = await SqliteUtil.open(appConfig.dbPath);
    try {
      let sqlWhere = '';
      let sqlParams = [(page.pageNum-1)*page.pageSize,page.pageSize];
      if(page.searchParams&&page.searchParams.roleName){
        sqlWhere +='where role_name=?'
        sqlParams.unshift(page.searchParams.roleName);
      }
      let result = await db.get(`select count(*) as totalNum from sys_role ${sqlWhere}`);
      let count = result.totalNum;
      let data = await SqliteUtil.each(db,`select id,role_name as roleName,sort from sys_role ${sqlWhere} order by create_time desc limit ?,?`,sqlParams);
      //加了await 会等待data拼装完成
      return PageResultVo.success('success',count,data);
    } catch (error) {
      throw new Error(error);
    } finally {
      await db.close();
    }   
  }
}
module.exports = SysRoleSqliteDao;